<?php

class Controller_Condominio extends Controller_Layout {
		
	public function action_create()
	{
		$this->template->title = __('Cadastro de condomínios');
  		$this->template->content = View::factory('condominios/create' );
		$this->template->content->estados = Transient_Estados::$siglas;
	}
	
	public function action_save()
	{
		$condominios = ORM::factory( 'condominio', arr::get ( $_POST, 'id' ) );
		parent::serializePost( $condominios, $_POST );
		$condominios->save();
		
		if( arr::get ( $_POST, 'id' ) )
			$this->request->redirect('condominio/list/?info=a'.$condominios->id);
		else
			$this->request->redirect('condominio/list/?info=s'.$condominios->id);
	}
	
	public function action_delete()
	{
		$id = $this->request->param('id');
		$condominios = ORM::factory('condominio', $id);
		$condominios->delete();
		
		$this->request->redirect('condominio/list/?info=e'.$id);
	}
	
	public function action_list()
	{
	
		$condominio = ORM::factory('condominio'); // ORM object
		
		// Prepare pagination
		$page = max($this->request->param('id'), 1);
		$items = Controller_Layout::$per_list; // Per page
		$offset = ($items * ($page - 1));
		
		// Object Pagination
		$pagination = Pagination::factory(array(
			'current_page'   => array('source' => 'route', 'key' => 'id'),
			'uri_segment'    => 'list',
			'total_items'    => $condominio->count_all(),
			'items_per_page' => $items,
		));
		
		$list_condominios = $condominio->order_by( 'nome', 'ASC' )->limit( $items )->offset( $offset )->find_all();
		
		$this->template->content = View::factory('condominios/list');
		$this->template->content->condominios = $list_condominios;
		$this->template->content->pagination = $pagination;
		
		$code = ( arr::get($_GET, "info") ) ? arr::get($_GET, "info") : null;
		if( $code ){
			$type = substr($code, 0, 1);
			$ormId = substr($code, 1);
			$msg = "";
			if( $type == "e" ){
				$msg = "Condomínio #".$ormId." excluído";	
			}else{
				$obj = ORM::factory( 'condominio', $ormId );
				$msg = "Condomínio #".$ormId.": ".$obj->nome;
				if( $type == "a" )
					$msg .= " alterado";
				else if( $type == "s" )
					$msg .= " cadastrado";
			}
			$msg .= " com sucesso!";
			$this->template->content->msg = $msg;
			$this->template->content->msgClass = Controller_Layout::$messagesClasses[$type]; //$this->request->post("msgClass");
		}
	}
		
	public function action_edit()
	{
		$id = $this->request->param('id');
		$this->template->content = View::factory('condominios/edit');
		$this->template->content->condominio = ORM::factory('condominio', $id);
		$this->template->content->estados = Transient_Estados::$siglas;
	}
	
	public function action_config()
	{
		$id = $this->request->param('id');
		$this->template->content = View::factory('condominios/config');
		$this->template->content->condominio = ORM::factory('condominio', $id);
	}
	
	public function action_adm()
	{
		$id = $this->request->param('id');
		$this->template->content = View::factory('condominios/adm');
		$condominio = ORM::factory('condominio', $id);
		$this->template->content->condominio = $condominio;
		$this->template->content->assembleias = $condominio->assembleias->find_all();
	}

}
